<template>
  <a-card class="page-main page-main-admin" :bordered="false" v-title data-title="字典表">
    <div class="container">
      <div class="hte-head-content">
        <span class="bold-font">数据字典</span>
      </div>
      <div class="content-admin">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
          <a-form layout="inline" @keyup.enter.native="searchQuery">
            <a-row :gutter="24">
              <a-col :span="8" class="hte-col-nowrap">
                <a-form-item label="模糊查询" prop="keyword">
                  <a-input v-model="queryParam.keyword" placeholder="请输入关键词"></a-input>
                </a-form-item>
              </a-col>
              <a-button type="primary" icon="search" @click="searchQuery">查询</a-button>
              <a-button type="default" style="margin-left: 16px" @click="searchReset">重置</a-button>
            </a-row>
          </a-form>
        </div>
        <!-- 查询区域-END -->

        <!-- 操作按钮区域 -->
        <div class="table-operator">
          <a-button @click="handleAdd" v-has="'manage:codeInfo:index'" type="primary" icon="plus">新增</a-button>
          <a-button type="primary" icon="download" @click="handleExportXls('t_code_info')">导出</a-button>
          <a-button v-has="'manage:codeInfo:index'" type="primary" icon="import" @click="importExcelDataBtn"
            >导入</a-button
          >
          <!-- <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload> -->
          <!-- 高级查询区域 -->
          <j-super-query
            :fieldList="superFieldList"
            ref="superQueryModal"
            @handleSuperQuery="handleSuperQuery"
          ></j-super-query>
          <a-dropdown v-if="selectedRowKeys.length > 0">
            <a-menu slot="overlay">
              <a-menu-item key="1" @click="batchDel" v-has="'manage:codeInfo:index'"
                ><a-icon type="delete" />删除</a-menu-item
              >
            </a-menu>
            <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
          </a-dropdown>
        </div>

        <!-- table区域-begin -->
        <div>
          <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
            <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
            <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
            >项
            <a style="margin-left: 24px" @click="onClearSelected">清空</a>
          </div>

          <a-table
            ref="table"
            size="middle"
            :scroll="{ x: true }"
            bordered
            rowKey="nid"
            :columns="columns"
            :dataSource="dataSource"
            :pagination="ipagination"
            :loading="loading"
            :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
            :customHeaderRow="customHeaderRow"
            class="j-table-force-nowrap"
            @change="handleTableChange"
          >
            <template slot="htmlSlot" slot-scope="text">
              <div v-html="text"></div>
            </template>
            <template slot="imgSlot" slot-scope="text">
              <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
              <img
                v-else
                :src="getImgView(text)"
                height="25px"
                alt=""
                style="max-width: 80px; font-size: 12px; font-style: italic"
              />
            </template>
            <template slot="fileSlot" slot-scope="text">
              <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
              <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
                下载
              </a-button>
            </template>

            <span slot="action" slot-scope="text, record">
              <a @click="handleEdit(record)" v-has="'manage:codeInfo:index'">编辑</a>

              <a-divider type="vertical" />
              <a-dropdown>
                <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
                <a-menu slot="overlay">
                  <a-menu-item>
                    <a @click="handleDetail(record)">详情</a>
                  </a-menu-item>
                  <a-menu-item>
                    <a-popconfirm
                      v-has="'manage:codeInfo:index'"
                      title="确定删除吗?"
                      @confirm="() => handleDelete(record.nid)"
                    >
                      <a>删除</a>
                    </a-popconfirm>
                  </a-menu-item>
                </a-menu>
              </a-dropdown>
            </span>
          </a-table>
        </div>
      </div>
    </div>

    <t-code-info-modal ref="modalForm" @ok="modalFormOk"></t-code-info-modal>
    <excel-paste
      :columnsExcel="columnsExcel"
      :exportXlsTemplateUrl="exportXlsTemplateUrl"
      :excelVisible="excelVisible"
      :excelConfirmLoading="excelConfirmLoading"
      @handleOkExcel="handleOkExcel"
      @handleCancelExcel="handleCancelExcel"
    ></excel-paste>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import TCodeInfoModal from './modules/TCodeInfoModal'
import ExcelPaste from '@/components/excelPaste/ExcelPaste'
import { httpAction, getAction, postAction } from '@/api/manage'
import { TableMixin } from '@/mixins/TableMixin'
export default {
  name: 'TCodeInfoList',
  mixins: [TableMixin,JeecgListMixin, mixinDevice],
  components: {
    TCodeInfoModal,
    ExcelPaste,
  },
  data() {
    return {
      isorter: {
        column: 'nid',
        order: 'desc',
      },
      description: 't_code_info管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: "60px",
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '类型',
          align: 'center',
          dataIndex: 'type',
          width:"200px",
        },
        {
          title: '编码',
          align: 'center',
          dataIndex: 'code',
          width:"100px",
        },

        {
          title: '编码名称',
          align: 'center',
          dataIndex: 'name',
          width:"200px",
        },
        {
          title: '附加字段',
          align: 'center',
          dataIndex: 'parentid',
          width:"200px",
        },
        {
          title: '附加字段2',
          align: 'center',
          dataIndex: 'additionalField',
          width:"200px",
        },
        {
          title: '附加字段3',
          align: 'left',
          dataIndex: 'additionalFieldThree',
          width:"200px",
          ellipsis: true,
          customCell: () => {
            return this.customCellRender(230)
          },
          customRender: (text, record) => {
            return this.tooltipRender(text, record)
          },
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/manage/codeInfo/list',
        delete: '/manage/codeInfo/delete',
        deleteBatch: '/manage/codeInfo/deleteBatch',
        exportXlsUrl: '/manage/codeInfo/exportXls',
        importExcelUrl: '/manage/codeInfo/importExcel',
        addBatch: '/manage/codeInfo/addBatch',
      },
      dictOptions: {},
      superFieldList: [],
      columnsExcel: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
          isImportExcelColumns: false,
        },
        {
          title: '类型',
          align: 'center',
          dataIndex: 'type',
        },
        {
          title: '编码',
          align: 'center',
          dataIndex: 'code',
        },
        {
          title: '编码名称',
          align: 'center',
          dataIndex: 'name',
        },
        {
          title: '附加字段',
          align: 'center',
          dataIndex: 'parentid',
        },
        {
          title: '附加字段2',
          align: 'center',
          dataIndex: 'additionalField',
        },
        {
          title: '附加字段3',
          align: 'center',
          dataIndex: 'additionalFieldThree',
        },
      ],
      excelVisible: false,
      excelConfirmLoading: false,
      excelDataSource: [],
      exportXlsTemplateUrl: 'templates/excel/数据字典模板.xlsx',
    }
  },
  created() {
    this.getSuperFieldList()
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    initDictConfig() {},
    getSuperFieldList() {
      let fieldList = []

      fieldList.push({ type: 'string', value: 'type', text: '类型' })
      fieldList.push({ type: 'string', value: 'code', text: '编码' })
      fieldList.push({ type: 'string', value: 'name', text: '编码名称' })
      fieldList.push({ type: 'string', value: 'parentid', text: '附加字段' })
      fieldList.push({ type: 'string', value: 'additionalField', text: '附加字段2' })
      fieldList.push({ type: 'string', value: 'additionalFieldThree', text: '附加字段3' })
      this.superFieldList = fieldList
    },
    handleOkExcel: function (data) {
      this.excelDataSource = []

      this.excelDataSource = data
      //数据验证
      this.excelConfirmLoading = true
      postAction(this.url.addBatch, data)
        .then((res) => {
          this.excelConfirmLoading = false
          if (res.code === 200) {
            this.excelVisible = false
            this.$message.success('导入成功')
            this.loadData()
          } else {
            this.$message.warning('操作异常:' + res.message)
          }
        })
        .catch((res) => {
          this.excelConfirmLoading = false
          this.$message.warning('操作异常:' + res.message)
        })
    },
    importExcelDataBtn: function () {
      this.excelVisible = true
    },
    handleCancelExcel: function () {
      this.excelVisible = false
    },
    customHeaderRow(column, index) {
      return {
        on: {
          click: () => {
            this.$message.info(
              '附加字段2(对于时间表固定节点的报工人，填oneAccount),附加字段3(现在用于时间表固定节点的工作完成内容)'
            )
          },
        },
      }
    },
    getQueryField() {
      //TODO 字段权限控制
      var str = 'nid,'
      this.columns.forEach(function (value) {
        str += ',' + value.dataIndex
      })
      return str
    },
  },
}
</script>
<style scoped lang="less">
@import '~@assets/less/common.less';
@import '~@assets/less/hte-main.less';
.table-operator {
  margin-top: 10px;
}
</style>
